'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 开服活动
 * @author sundbcn@gmail.com (sun debing)
 */

var moment = require('moment');
var async = require('async');
var modelUtil = require('../util/modelUtil');
var configUtil = require('../util/configUtil');
var constantUtil = require('../util/constantUtil');
var mongoConnector = require('../libs/mongodb/mongoConnector');
/* jshint unused:false */
var activitySchema = require('../schemas/activitySchema');
var Activity = mongoConnector().model('Activity');

/**
 * 根据角色id获取开服活动数据
 */
module.exports.getByRoleId = function (role_id, cb) {
    modelUtil.getByRoleId(Activity, role_id, function (err, activity_model) {
        if (!!err) {
            console.error(err);
            return cb(err);
        }
        cb(null, activity_model);
    });
};

/**
 * 刷新数据
 */
module.exports.onRefreshData = function (self, cb) {
    var open_game_time = configUtil.getOpenGameTime();
    var now_tm = moment().unix();
    var day_num = Math.ceil((now_tm - open_game_time + 1) / configUtil.second_per_day);
    var activity_days = constantUtil.value('open_service_time');
    if (day_num <= activity_days) {
        // 当天是否已经购买
        if (now_tm >= self.next_refresh_limit_buy_tm) {
            self.limit_buy_remain_times = constantUtil.value('limit_buy_times_per_day');
            self.next_refresh_limit_buy_tm = open_game_time + day_num * configUtil.second_per_day;
        }
    } else if (self.limit_buy_remain_times > 0) {
        self.limit_buy_remain_times = 0;
    }
    cb();
};
